<?php

$process = new swoole_process(function (\Swoole\Process $worker){
    swoole_event_add($worker->pipe,function($pipe)use($worker){
        sleep(3);
        //子进程逻辑
        //通过管道从主进程读取数据
        $cmd= $worker->read();
        ob_start();
        //执行外部程序并显示
        passthru($cmd);
        $ret = ob_get_clean() ?:'';
        $ret= trim($ret).". worker pid:".$worker->pid. "\n";
        $worker->write($ret);
        $worker->exit(0);
    });
});

// 启动进程
$process->start();
// 从主进程将通过管道发送数据到子进程
$process->write('php --version');
// 从子进程读取返回数据并打印
$msg = $process->read();
echo 'result from worker: ' . $msg;